home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Papers / C++ Exceptions / µShell / Toolbox / AppleEventManager.cp next >
Encoding:
Text File  |  1998-06-15  |  2.6 KB  |  124 lines  |  [TEXT/CWIE]

  1. #include "AppleEventManager.h"
  2. #include "ThreadManager.h"
  3. #include "ToolboxEvent.h"
  4. #include "Exceptions.h"
  5. #include "EventLoop.h"
  6. #include "Futures.h"
  7.  
  8. //--------------------------------------------------------------------------------
  9.  
  10. #if qDebug
  11.  
  12.     AppleEventManager& AppleEventManager::GetAppleEventManager()
  13.     {
  14.         if (!gAppleEventManager)
  15.         {
  16.             Debugger();
  17.         }
  18.  
  19.         return *gAppleEventManager;
  20.     }
  21.  
  22. //--------------------------------------------------------------------------------
  23.  
  24.     AppleEventManager::AppleEventManager()
  25.     {
  26.         WarnIf(gAppleEventManager != nil, "Instantiating a second AppleEventManager");
  27.  
  28.         gAppleEventManager = this;
  29.     }
  30.  
  31. #endif
  32.  
  33. //--------------------------------------------------------------------------------
  34.  
  35. AppleEventManager::~AppleEventManager()
  36. {
  37.     gAppleEventManager = nil;
  38. }
  39.  
  40. //--------------------------------------------------------------------------------
  41.  
  42. bool AppleEventManager::Validate()
  43. {
  44.     if (TModule::Validate())
  45.     {
  46.         return true;
  47.     }
  48.     
  49.     return false;
  50. }
  51.  
  52. //--------------------------------------------------------------------------------
  53.  
  54. void AppleEventManager::Initialize(void)
  55. {
  56.     InitializeAfter(ThreadManager::GetThreadManager());
  57.  
  58.     TModule::Initialize();
  59.     EventFilter::Initialize();
  60.  
  61.     #if qFeatureSupported(qFuturesSupport)
  62.         FailOSErr(InitFutures(nil, kNoSpecialFutureFeatures));
  63.     #endif
  64. }
  65.  
  66. //--------------------------------------------------------------------------------
  67.  
  68. void AppleEventManager::Finalize(void)
  69. {
  70.     TModule::Finalize();
  71.     EventFilter::Finalize();
  72. }
  73.  
  74. //--------------------------------------------------------------------------------
  75.  
  76. bool AppleEventManager::FilterEvent(ToolboxEvent& event)
  77. {
  78.     #if qFeatureSupported(qFuturesSupport)
  79.         IdleFutures();
  80.     #endif
  81.     
  82.     if (event.IsHighLevelEvent())
  83.     {
  84.         FailOSErr(::AEProcessAppleEvent(event));
  85.         
  86.         return true;
  87.     }
  88.     
  89.     return false;
  90. }
  91.  
  92. //--------------------------------------------------------------------------------
  93.  
  94. void AppleEventManager::SendAppleEvent(
  95.     const AppleEvent&    theAppleEvent,
  96.     AppleEvent*            reply,
  97.     long                timeOutInTicks,
  98.     long                maxWaitTime,
  99.     AESendMode            sendMode,
  100.     AESendPriority         sendPriority)
  101. {
  102.     OSErr err;
  103.     
  104.     if (qFeatureSupported(qFuturesSupport) 
  105.     &&    ((sendMode & 0x03) == kAEFutureReply))
  106.     {
  107.         if (maxWaitTime == kNoTimeOut)
  108.         {
  109.             maxWaitTime = kNoMaximumWait;
  110.         }
  111.     
  112.         err = AskForFuture(&theAppleEvent, reply, timeOutInTicks, maxWaitTime, sendMode, sendPriority);
  113.     }
  114.     else
  115.     { 
  116.         err = AESend(&theAppleEvent, reply, sendMode, sendPriority, timeOutInTicks, EventLoop::GetAEIdleProc(), EventLoop::GetAEFilterProc());
  117.     }
  118.  
  119.     FailOSErr(err);
  120. }
  121.  
  122. //--------------------------------------------------------------------------------
  123.  
  124.